package com.feihong.yw189.utils;

import android.text.TextUtils;

import com.feihong.yw189.global.GlobalConfig;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author wangziheng
 * @name Android
 * @class name：com.feihong.yw189.utils
 * @class describe:
 * @time 2018/1/20 19:42
 * @change
 * @chang time
 * @class describe
 */
public class ComPareUtils {
    private Map<String, String> mParams;

    private ComPareUtils(ComPareBuilder builder) {
        mParams = builder.mParams;
    }

    public static class ComPareBuilder {
        private TreeMap<String, String> mParams = new TreeMap<String, String>(
                new Comparator<String>() {
                    @Override
                    public int compare(String obj1, String obj2) {
                        // 升序排序
                        return obj1.compareTo(obj2);
                    }
                });

        public ComPareBuilder put(String key, String value) {
            if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                this.mParams.put(key, value);
            }
            return this;
        }

        public ComPareBuilder putMap(Map<String,String> map){
            Set<String> keySet = map.keySet();
            for(String key : keySet){
                if(!"token".equals(key) && !TextUtils.isEmpty(key)){
                    put(key,map.get(key));
                }
            }
            return this;
        }

        public ComPareUtils create() {
            Set<String> keySet = mParams.keySet();
            Iterator<String> iter = keySet.iterator();
            StringBuilder stringBuilder = new StringBuilder();
            while (iter.hasNext()) {
                String key = iter.next();
                stringBuilder.append(mParams.get(key));
            }
            return new ComPareUtils(this);
        }
    }

    public String getSignString(){
        if(mParams == null){
            throw new NullPointerException("未传入加密参数");
        }
        Set<String> keySet = mParams.keySet();
        Iterator<String> iter = keySet.iterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iter.hasNext()) {
            String key = iter.next();
            stringBuilder.append(mParams.get(key));
        }
      //  return MyLoginUtils.encode(stringBuilder.toString() + GlobalConfig.PUBLIC_KEY);
        return   stringBuilder.toString() + GlobalConfig.PUBLIC_KEY;
    }
}
